package com.campus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.entity.Teacher;
import com.campus.entity.vo.Teacher.TeacherVO;
import com.campus.mapper.TeacherMapper;
import com.campus.service.TeacherService;
import com.campus.util.CopyPropertiesUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alaskaboo
 * @description 针对表【t_teacher(教师表)】的数据库操作Service实现
 * @createDate 2022-10-24 11:09:59
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public IPage<TeacherVO> selectTeacherList(Page<Teacher> page, Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacher.getName())) {
            queryWrapper.like("name", teacher.getName());
        }
        if (!StringUtils.isEmpty(teacher.getAccount())) {
            queryWrapper.like("account", teacher.getAccount());
        }
        if (!StringUtils.isEmpty(teacher.getIdentity())) {
            queryWrapper.like("identity", teacher.getIdentity());
        }
        if (!StringUtils.isEmpty(teacher.getTel())) {
            queryWrapper.like("tel", teacher.getTel());
        }
        queryWrapper.orderByAsc("id");
        List<Teacher> teacherList = baseMapper.selectList(queryWrapper);
        List<TeacherVO> collect = teacherList.stream().map(teacher1 -> CopyPropertiesUtils.copyProperties(teacher1, TeacherVO.class)).collect(Collectors.toList());
        Page<Teacher> page1 = baseMapper.selectPage(page, queryWrapper);
        Page<TeacherVO> resPage = new Page<>();
        BeanUtil.copyProperties(page1,resPage);
        return resPage.setRecords(collect);
    }
}




